var UILoading, ns, ajpush, count = 0,
    toast;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            account: '',
            pwd: '',
        },
        device_type: '',
        device_id: '',
        logoImagePath: '../icon/login_bg_font.png',
        // logoImagePath: '../icon/login_logo.png',
        hasSysNumber: false
    },
    methods: {
        get_data: function () {
            var that = this;
            that.get_jiguang_register_id();
        },
        do_login: function () {
            var that = this;
            if (!that.form.account) {
                api.toast({
                    msg: '请输入登录名',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.pwd) {
                api.toast({
                    msg: '请输入密码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            console.log(JSON.stringify(that.form));
            ns.post(that.form, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    if (data.companyname) {
                        api.setPrefs({
                            key: 'companyName',
                            value: data.companyname
                        });
                    }
                    if (data.token) {
                        api.setPrefs({
                            key: 'uid',
                            value: data.token
                        });
                    }
                    if (that.form.account) {
                        api.setPrefs({
                            key: 'loginname',
                            value: that.form.account
                        });
                    }
                    if (data.userName) {
                        api.setPrefs({
                            key: 'username',
                            value: data.userName
                        });
                    }
                    if (data.avator) {
                        api.setPrefs({
                            key: 'headimg',
                            value: data.avator
                        });
                    }
                    if (data.phoneNumber) {
                        api.setPrefs({
                            key: 'phonenumber',
                            value: data.phoneNumber
                        });
                    }
                    if (data.departmentName) {
                        api.setPrefs({
                            key: 'positionname',
                            value: data.departmentName
                        });
                    }
                    api.sendEvent({
                        name: 'onlogin'
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            },{
                ignoreuid: true,
                command: 'App/Auth/Login'
            });
        },
        get_jiguang_register_id: function () {
            var that = this;
            ajpush.getRegistrationId(function (res, err) {
                if (res && res.id) {
                    that.device_id = res.id;
                    api.setPrefs({
                        key: 'jpush_device_id',
                        value: res.id
                    });
                }
            });
        },
        set_status_bar: function () {
            var that = this;
            api.setStatusBarStyle({
                style: 'dark',
                color: 'transparent'
            });
        }
    }
});

apiready = function () {
    UILoading = api.require('UILoading');
    ns = window.Foresight.Util;
    ajpush = api.require('ajpush');
    toast = new auiToast();
    app.set_status_bar();
    app.get_jiguang_register_id();
    app.device_type = api.systemType;
    app.get_data();
}
